import component from "*.vue";
import { createRouter, createWebHistory } from "vue-router";
import * as service from "../service";
import { getCookie } from "../utils";

const Home = () =>
  import(/*webpackChunkName: "yzc"*/ "../pages/Home/index.vue");
const Cate = () =>
  import(/*webpackChunkName: "Cate"*/ "../pages/Cate/index.vue");
const PostAge = () =>
  import(/*webpackChunkName="postage"*/ "@/pages/PostAge/index.vue");
const Crazy = () =>
  import(/*webpackChunkName="crazy"*/ "@/pages/Crazy/index.vue");
const Half = () =>
  import(/*webpackChunkName="discount"*/ "@/pages/Half/index.vue");
const CateList = () =>
  import(/*webpackChunkName: "Cate"*/ "../pages/CateList/index.vue");
const Mine = () =>
  import(/*webpackChunkName: "mine"*/ "../pages/Mine/index.vue");
const Login = () =>
  import(/*webpackChunkName: "yzc"*/ "../pages/Login/index.vue");
const Register = () =>
  import(/*webpackChunkName: "yzc"*/ "../pages/Register/index.vue");
const Pick = () =>
  import(/*webpackChunkName: "yzc"*/ "../pages/Pick/index.vue");
const Ticket = () =>
  import(/*webpackChunkName: "yzc"*/ "../pages/Ticket/index.vue");
const Discount = () =>
  import(/*webpackChunkName: "yzc"*/ "../pages/Discount/index.vue");
const Brand = () =>
  import(/*webpackChunkName: "yzc"*/ "../pages/Brand/index.vue");
const NotFound = () =>
  import(/*webpackChunkName: "notFound"*/ "../pages/NotFound/index.vue");
const Food = () =>
  import(/*webpackChunkName: "Food"*/ "../pages/Food/index.vue");
const Mother = () =>
  import(/*webpackChunkName: "Mother"*/ "../pages/Mother/index.vue");
const Search = () =>
  import(/*webpackChunkName: "Mother"*/ "../pages/Search/index.vue");
const router = createRouter({
  routes: [
    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "/home",
      name: "home",
      component: Home,
    },
    {
      path: "/cate",
      name: "cate",
      component: Cate,
    },
    {
      path: "/catelist/:cid",
      name: "catelist",
      component: CateList,
      meta: {},
    },
    {
      path: "/food/:cid",
      name: "food",
      component: Food,
    },
    {
      path: "/mother",
      name: "mother",
      component: Mother,
      meta: {
        title: "辣妈优选",
      },
    },
    {
      path: "/search",
      name: "search",
      component: Search,
    },
    {
      path: "/postage",
      component: PostAge,
      name: "postage",
    },
    {
      path: "/crazy",
      component: Crazy,
      name: "crazy",
    },
    {
      path: "/mine",
      component: Mine,
      name: "mine",
    },
    {
      path: "/login",
      component: Login,
      name: "login",
    },
    {
      path: "/register",
      component: Register,
      name: "register",
    },
    {
      path: "/pick",
      component: Pick,
      name: "pick",
      meta: {
        title: "618精选好货",
      },
    },
    {
      path: "/ticket",
      component: Ticket,
      name: "ticket",
      meta: {
        title: "大额券热卖清单",
      },
    },
    {
      path: "/half",
      name: "half",
      component: Half,
    },
    {
      path: "/discount",
      name: "discount",
      component: Discount,
    },
    {
      path: "/brand",
      name: "brand",
      component: Brand,
    },
    {
      path: "/:pathMatch(.*)*",
      name: "NotFound",
      component: NotFound,
    },
  ],
  history: createWebHistory(),
});

router.beforeEach(async (to, from, next) => {
  const token = getCookie("token");
  if (token) {
    const { status } = await service.fetchToken(token);
    if (status === 200 || to.path === "/login" || to.path === "/register") {
      next();
    } else {
      next("/login");
    }
  } else {
    if (to.path === "/login" || to.path === "/register") {
      next();
    } else {
      next("/login");
    }
  }
});

export default router;
